package com.kk.permission.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.kk.R;
import com.kk.permission.holder.PermissionStepViewHolder;

import java.util.ArrayList;
import java.util.List;

public class PermissionStepAdapter extends RecyclerView.Adapter {

    public static final int UPDATE_STATUS = 1;

    private Context context;
    private List<ItemData> stepList = new ArrayList<>();

    class ItemData {
        private String name;
        private boolean isDone;

        ItemData(String name) {
            this.name = name;
            this.isDone = false;
        }

        public String getName() {
            return name;
        }

        public boolean isDone() {
            return isDone;
        }

        public void setDone(boolean done) {
            isDone = done;
        }
    }

    public PermissionStepAdapter(@NonNull Context context, @NonNull List<String> list) {
        this.context = context;
        stepList.clear();
        for (String name : list) {
            stepList.add(new ItemData(name));
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int position) {
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        return new PermissionStepViewHolder(layoutInflater.inflate(R.layout.acc_layout_step_item, parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (stepList == null || stepList.isEmpty())
            return;

        if (position >= stepList.size() || position < 0)
            return;

        PermissionStepViewHolder stepViewHolder = (PermissionStepViewHolder)viewHolder;
        stepViewHolder.itemName.setText(stepList.get(position).getName());
        stepViewHolder.setStatus(stepList.get(position).isDone(), false);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull List payloads) {
        if (stepList == null || stepList.isEmpty())
            return;

        if (position >= stepList.size() || position < 0)
            return;

        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            PermissionStepViewHolder plvh = (PermissionStepViewHolder)holder;
            int type = (int)payloads.get(0);
            switch (type) {
                case UPDATE_STATUS: //only update status
                    plvh.setStatus(stepList.get(position).isDone(), true);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return stepList.size();
    }

    public void setStatusDone(int position) {
        if (stepList == null || stepList.isEmpty())
            return;

        if (position >= stepList.size() || position < 0)
            return;

        stepList.get(position).setDone(true);

    }
}
